package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;

import com.filter.MyErrorFilter;
import com.filter.MyFirstPostFilter;
import com.filter.MyPreFilter;
import com.filter.MyRouteFilter;
import com.filter.MySecondPostFilter;
import com.netflix.zuul.FilterFileManager;
import com.netflix.zuul.FilterLoader;
import com.netflix.zuul.groovy.GroovyCompiler;
import com.netflix.zuul.groovy.GroovyFileFilter;

@ConfigurationProperties("zuul.filter")
@EnableZuulProxy
@SpringBootApplication
public class ZuulApp 
{
	private String root;
	private String interval;
	
	public String getRoot() {
		return root;
	}


	public void setRoot(String root) {
		this.root = root;
	}


	public String getInterval() {
		return interval;
	}


	public void setInterval(String interval) {
		this.interval = interval;
	}


	@Bean
	public FilterLoader refreshZuulFilter() {
        FilterLoader zuulFilterLoader = FilterLoader.getInstance();
        zuulFilterLoader.setCompiler(new GroovyCompiler());
        
        System.out.println(this.getRoot());
        System.out.println(this.getInterval() );
        
        try {
            FilterFileManager.setFilenameFilter(new GroovyFileFilter());
            FilterFileManager.init(Integer.valueOf(getInterval()), this.getRoot()+ "/pre", 
            		this.getRoot() + "/route", this.getRoot() + "/post");
        } catch (Exception e) {
            e.printStackTrace();;
        }
        
        return zuulFilterLoader;
	}
	
	
	@Bean
	public MyPreFilter myPreRequestFilter() {
	 return new MyPreFilter();
	}
	
	@Bean
	public MyRouteFilter myPreRouteFilter() {
	 return new MyRouteFilter();
	}
	
	@Bean
	public MyFirstPostFilter myFirstPostFilter() {
	 return new MyFirstPostFilter();
	}
	
	@Bean
	public MySecondPostFilter mySecondPostFilter() {
	 return new MySecondPostFilter();
	}
	
	@Bean
	public MyErrorFilter myErrorFilter() {
	 return new MyErrorFilter();
	}
	
	
	
	public static void main( String[] args )
    {
    	SpringApplication.run(ZuulApp.class, args);    	
    }
}
